/*
	This file is part of GTimer.

    GTimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License\.

    GTimer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GTimer. If not, see <http://www.gnu.org/licenses/>.
	
	Copyright 2012 Muhammad Ghareeb Sideek
	Contact: MGhareeb64@gmail.com
 */

package com.gsourcecode.command;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

import android.graphics.Color;

public class Command implements Cloneable, Serializable {

	private static final long serialVersionUID = 4441038072753732384L;

	// PUBLIC

	/** returns a GTimer bad file-like **/
	public static ArrayList<Command> wrongList() {
		ArrayList<Command> out = new ArrayList<Command>();
		Command c = new Command(Long.MAX_VALUE, Color.RED, "\"Bad File!\"", "Check your GTimer file.");
		out.add(c);
		return out;
	}

	/** save to GTimer file **/
	public static void toFile(ArrayList<Command> cmd, File f) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(f));
			for (int i = 0; i < cmd.size(); i++, out.newLine()) {
				cmd.get(i).time /= 1000;
				out.write(cmd.get(i).toString());
			}
			out.close();
		} catch (Exception e) {}
	}

	/** parese a GTimer file **/
	public static ArrayList<Command> parseFile(File f) throws FileNotFoundException {
		ArrayList<Command> out = parseFile(new Scanner(f));
		for (int i = 1; i < out.size(); i++)
			out.get(i).time += out.get(i - 1).time;
		return out;
	}

	/** parese a GTimer from a Scanner **/
	private static ArrayList<Command> parseFile(Scanner s) {
		ArrayList<Command> out = new ArrayList<Command>();
		String line;
		while (s.hasNext()) {
			line = s.nextLine();
			switch (line.charAt(0)) {
			case '*':
				break;
			case 'l':
			case 'L':
				int n = Integer.parseInt(line.substring(4).trim());
				ArrayList<Command> body = parseFile(s);
				for (int i = 0; i < n; i++)
					for (Command c : body)
						out.add(c.clone());
				break;
			case 'p':
			case 'P':
				return out;
			default:
				out.add(new Command(new Scanner(line)));
			}
		}
		return out;
	}

	// COMMAND

	public long time = 0;
	public int color = Color.BLACK;
	public String title = "", sub = "";

	public Command(long time, int color, String text, String sub) {
		this.time = time;
		this.color = color;
		this.title = text;
		this.sub = sub;
	}

	/** read command **/
	public Command(Scanner s) {
		time = ((s.nextLong() * 60 + s.nextLong()) * 60 + s.nextLong()) * 1000;
		color = Color.parseColor(s.next());
		do
			title += " " + s.next();
		while (title.charAt(title.length() - 1) != '\"');
		title = title.substring(2, title.length() - 1);
		if (title.contains("_")) {
			sub = title.substring(title.indexOf("_") + 1, title.length());
			title = title.substring(0, title.length() - sub.length() - 1);
		}
	}

	public Command(String s) {
		this(new Scanner(s));
	}

	@Override
	protected Command clone() {
		return new Command(time, color, title, sub);
	}

	@Override
	public String toString() {
		String out = "";
		out += String.format("%02d", (time / 60 / 60)) + " ";
		out += String.format("%02d", (time / 60) % 60) + " ";
		out += String.format("%02d", (time) % 60) + " ";
		out += String.format("#%06X", color) + "\t\"";
		out += title + "_" + sub + "\"";
		return out;
	}
}
